package cn.fanxc.fogandhaze.service.impl;

import cn.fanxc.fogandhaze.service.Service;
import cn.fanxc.fogandhaze.utils.URLUtil;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ServiceImpl
 * @Description 接口实现类
 * @Author Fanxc
 * @DATE 2021/5/3 21:00
 * @Version 1.0
 */

@org.springframework.stereotype.Service
public class ServiceImpl implements Service {

    private static final String url1 = "https://www.tianqiapi.com/free/day";
    private static final String url2 = "https://v0.yiketianqi.com/api";
    private static final String url3 = "https://www.tianqiapi.com/free/week";

    public static final String appid = "93739564";

    public static final String appsecret = "u6K9bDyr";


    @Override
    public JSONObject getUrbanAirQuality(String city) {

        Map<String,String> params = new HashMap<>();
        params.put("city",city);    //城市的中文名称或拼音
        params.put("appsecret",appsecret);
        params.put("appid",appid);
        return get(city,url1,params);

    }

    @Override
    public JSONObject getAirQuality(String city) {
        Map<String,String> params = new HashMap<>();
        params.put("city",city);    //城市的中文名称或拼音
        params.put("appsecret",appsecret);
        params.put("appid",appid);
        params.put("version","v10");
        return get(city,url2,params);
    }

    @Override
    public JSONObject getT7(String city) {
        Map<String,String> params = new HashMap<>();
        params.put("city",city);    //城市的中文名称或拼音
        params.put("appsecret",appsecret);
        params.put("appid",appid);
        return get(city,url3,params);
    }

    public JSONObject get(String city, String url,Map<String,String> params) {
        JSONObject jsonObject = null;

        if(city==null){
            try{
                String result = URLUtil.net(url,params,"GET");
                jsonObject = JSONObject.fromObject(result);
            }catch (Exception e)
            {
                e.printStackTrace();
            }
            return jsonObject;
        }

        String result = null;
        try{
            result = URLUtil.net(url,params,"GET");
            jsonObject = JSONObject.fromObject(result);
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return jsonObject;
    }
}
